package systeminfo

import (
	"fmt"
	"io/ioutil"
	"strconv"
	"strings"
)

func GetRamStatus() (ram Ram) {
	contents, err := ioutil.ReadFile("/proc/meminfo")
	if err != nil {
		return
	}
	lines := strings.Split(string(contents), "\n")
	for _, line := range lines {
		switch {
		case strings.Contains(line, "MemTotal") == true:
			ram.Totalram = fmt.Sprint(getCount(line) / 1024)

		case strings.Contains(line, "MemAvailable") == true:
			ram.Freeram = fmt.Sprint(getCount(line) / 1024)

		case strings.Contains(line, "SwapTotal") == true:
			ram.Totalswap = fmt.Sprint(getCount(line) / 1024)

		case strings.Contains(line, "SwapFree") == true:
			ram.Freeswap = fmt.Sprint(getCount(line) / 1024)
		}
	}
	return
}

func getCount(s string) (v int) {
	temp := strings.Split(s, ":")[1]
	temp = strings.Trim(temp, "kB")
	temp = strings.Trim(temp, ":")
	temp = strings.Trim(temp, " ")
	v, err := strconv.Atoi(temp)
	if err != nil {
		fmt.Println(err)
	}
	return
}
